package com.haixiaoke.flat.user.controller;


import cn.hutool.core.util.StrUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import com.haixiaoke.saas.user.domain.User;
import com.haixiaoke.saas.user.service.IUserService;
import com.haixiaoke.token.FrontTokenComponent;
import com.haixiaoke.token.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户Controller
 *
 * @author xiaolong
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/front/flat/user/user")
public class FlatUserController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 获取用户详细信息
     */
    @GetMapping
    @AuthCheck
    public AjaxResult getInfo() {
        return success(userService.selectUserByUserId(frontTokenComponent.getUserIdNotNull()));
    }

    /**
     * 忘记密码/注册
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody User user) {
        return toAjax(userService.register(user));
    }

    /**
     * 设置用户密码
     */
    @PutMapping("/password/set")
    @AuthCheck
    public AjaxResult setPassword(@RequestBody User user) {
        return toAjax(userService.setPassword(user));
    }

    /**
     * 重置密码
     */
    @PutMapping("/password/reset")
    @AuthCheck
    public AjaxResult resetPassword(@RequestBody User user) {
        if (StrUtil.isEmpty(user.getOldPassword())) {
            return error("旧密码不能为空");
        }
        if (!SecurityUtils.matchesPassword(user.getOldPassword(), frontTokenComponent.getLoginUser().getUser().getPassword())) {
            return error("旧密码错误");
        }
        return toAjax(userService.resetPassword(user));
    }

    /**
     * 修改用户
     */
    @PutMapping
    @AuthCheck
    public AjaxResult edit(@RequestBody User user) {
        int rows = userService.updateUser(user);
        if (rows > 0) {
            LoginUser loginUser = frontTokenComponent.getLoginUser();
            User sqlUser = loginUser.getUser();
            sqlUser.setUserName(StrUtil.isNotEmpty(user.getUserName()) ? user.getUserName() : sqlUser.getUserName());
            sqlUser.setPhone(StrUtil.isNotEmpty(user.getPhone()) ? user.getPhone() : sqlUser.getPhone());
            sqlUser.setProfilePhoto(StrUtil.isNotEmpty(user.getProfilePhoto()) ? user.getProfilePhoto() : sqlUser.getProfilePhoto());
            frontTokenComponent.refreshToken(loginUser);
        }
        return toAjax(rows);
    }

    /**
     * 注销用户
     */
    @DeleteMapping("/logoutUser")
    @AuthCheck
    public AjaxResult logoutUser() {
        return toAjax(userService.logoutUser(frontTokenComponent.getUserIdNotNull(), frontTokenComponent.getLoginUser().getUser().getUserName()));
    }
}
